package com.mini.cn.goods.io.netty.ws;

import com.mini.cn.goods.io.netty.io.NettyServer;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.HashSet;
import java.util.List;

/**
 * Created with IntelliJ IDEA.

 * User: fly
 * Date: 2020-01-16
 * Time: 7:19 下午
 */
public final class WebSocketNettyServer extends NettyServer {

    private final WebSocketChannelInitializer webSocketChannelInitializer;
    private int port;
    private final HashSet<String> paths;
    private final HttpUpgradeHandler httpUpgradeHandler;


    public WebSocketNettyServer(IWsHandel wsHandel, List<ChannelHandler> privateHandle) {
        this.port = 8888;
        paths = new HashSet<String>();
        httpUpgradeHandler = new HttpUpgradeHandler(wsHandel, paths, privateHandle);
        webSocketChannelInitializer =
                new WebSocketChannelInitializer(httpUpgradeHandler);
    }

    @Override
    public ChannelInitializer<NioSocketChannel> initChannelInitializer() {
        return webSocketChannelInitializer;
    }

    @Override
    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void addPaths(String[] pathstrs) {
        for (String path : pathstrs) {
            paths.add(path);
        }
    }
}
